package structures.kys;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import exception.IBToolsException;

import manager.LoadNativeTableManager;

public class KysQueryParameterList {

	public static final Map<String, String> PARAMETERS = new HashMap<String, String>(){{
		put("STRING" , "1");
		put("INT" , "2");
		put("LONG" , "3");
		put("DOUBLE" , "4");
		put("BIGDECIMAL" , "5");
		put("CBCURRENCY" , "6");
		put("CBDATE" , "7");
		put("CBTIME" , "8");
		put("CBACCOUNTID" , "9");
	}};
	
	public static List<KysQueryParameter> loadParameters(String definitionString) throws IBToolsException{
		List<KysQueryParameter> kysQueryParameterList = new ArrayList<KysQueryParameter>();
		String[] definitionArr = definitionString.split("\n");
		for (String definitionLine : definitionArr) {
			definitionLine = definitionLine.replaceAll("\r", "");
			String[] defLineArr = definitionLine.split(",");
			String[] orderArr = defLineArr[0].split("-");
			int[] orders = loadOrders(orderArr);
			for (int order : orders) {
				kysQueryParameterList.add(new KysQueryParameter(
						defLineArr[1], 	// ioytpe
						defLineArr[2], 	// paramName
						PARAMETERS.get(defLineArr[3]), 	// paramType
						order + "", 	// order
						defLineArr[4], 	// defaultValue
						LoadNativeTableManager.loadQueryOid(defLineArr[5]))	// mainId
						);
			}
		}
	
		return kysQueryParameterList;
	}
	
	private static int[] loadOrders(String[] orderArr){
		int[] orders = null;
		int beginOrder = Integer.parseInt(orderArr[0]);
		if(orderArr.length > 1){
			int endOrder = Integer.parseInt(orderArr[1]);
			orders = new int[endOrder - beginOrder + 1];
			int index=0;
			for (int i = beginOrder; i < endOrder + 1; i++) {
				orders[index] = i;
				index ++;
			}
		}
		else {
			orders = new int[1];
			orders[0] = beginOrder;
		}
		return orders;
	}
}
